#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 105;

int n, m;
vector<string> articles[105];
map<string, set<int>> w2a;
map<string, set<int>> w2l[105];

vector<string> parse(string s) {
  vector<string> res;
  int l = 0;
  int len = s.size();
  while (l < len) {
    if (!isalpha(s[l])) {
      l++;
      continue;
    }
    int h = l + 1;
    while (h < len && isalpha(s[h])) h++;
    string word;
    rep(i, l, h - 1) word.push_back(tolower(s[i]));
    res.push_back(word);
    l = h;
  }
  return res;
}
void read_article(int id) {
  string line;
  int lineid = 0;
  while (getline(cin, line) && line != "**********") {
    articles[id].push_back(line);
    vector<string> words = parse(line);
    for (string word : words) {
      w2a[word].insert(id);
      w2l[id][word].insert(lineid);
    }
    ++lineid;
  }
}
void solve_word(string word) {
  set<int> st = w2a[word];
  bool start = true;
  for (int id : st) {
    if (start)
      start = !start;
    else
      cout << "----------" << endl;
    set<int> lineids = w2l[id][word];
    for (int l : lineids) cout << articles[id][l] << endl;
  }
  if (st.empty()) cout << "Sorry, I found nothing." << endl;
  cout << "==========" << endl;
}
void solve_not(string word) {
  set<int> st = w2a[word];
  bool start = true;
  rep(i, 1, n) {
    if (st.count(i)) continue;
    if (start)
      start = !start;
    else
      cout << "----------" << endl;
    for (string line : articles[i]) cout << line << endl;
  }
  if (st.size() == n) cout << "Sorry, I found nothing." << endl;
  cout << "==========" << endl;
}
set<int> setand(set<int> a, set<int> b) {
  set<int> c;
  for (int x : a)
    if (b.count(x)) c.insert(x);
  return c;
}
set<int> setor(set<int> a, set<int> b) {
  set<int> c;
  for (int x : a) c.insert(x);
  for (int x : b) c.insert(x);
  return c;
}
void solve_and(string a, string b) {
  set<int> sta = w2a[a];
  set<int> stb = w2a[b];
  set<int> stc = setand(sta, stb);
  bool start = true;
  for (int id : stc) {
    if (start)
      start = !start;
    else
      cout << "----------" << endl;
    set<int> st1 = w2l[id][a];
    set<int> st2 = w2l[id][b];
    set<int> st3 = setor(st1, st2);
    for (int j : st3) cout << articles[id][j] << endl;
  }
  if (stc.empty()) cout << "Sorry, I found nothing." << endl;
  cout << "==========" << endl;
}
void solve_or(string a, string b) {
  set<int> sta = w2a[a];
  set<int> stb = w2a[b];
  set<int> stc = setor(sta, stb);
  bool start = true;
  for (int id : stc) {
    if (start)
      start = !start;
    else
      cout << "----------" << endl;
    set<int> st1 = w2l[id][a];
    set<int> st2 = w2l[id][b];
    set<int> st3 = setor(st1, st2);
    for (int j : st3) cout << articles[id][j] << endl;
  }
  if (stc.empty()) cout << "Sorry, I found nothing." << endl;
  cout << "==========" << endl;
}
void solve(string line) {
  vector<string> words = parse(line);
  int cnt = words.size();
  if (cnt == 1) solve_word(words[0]);
  if (cnt == 2) solve_not(words[1]);
  if (cnt == 3 && words[1] == "and") solve_and(words[0], words[2]);
  if (cnt == 3 && words[1] == "or") solve_or(words[0], words[2]);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("1.txt", "r", stdin);
  // freopen("2.txt", "w", stdout);
  string line;
  cin >> n;
  getline(cin, line);
  rep(i, 1, n) read_article(i);
  cin >> m;
  getline(cin, line);
  while (m--) {
    getline(cin, line);
    solve(line);
  }
  return 0;
}